import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.math.*;

/*
 * This class represents the graph panel
 */

public class GraphPanel extends JPanel{
	
	public Graphics canvas;
	
	//constructor
	public GraphPanel() {
        this.setPreferredSize(new Dimension(1000, 500));
        this.setVisible(true);
        this.setBackground(Color.BLACK);
	}
	 
	//this function generates the probability graph
	public void paintComponent(Graphics canvasGraphics){
		canvas = canvasGraphics;
		canvas.setColor(Color.BLACK);
		canvas.fillRect(50, 50, 900, 400);
		canvas.setColor(Color.GREEN);
		int xStart=50;
		int yStart=450;
		int xEnd=950;
		int yEnd=50;
		int levelSpan=900/SplitSim.splitCount;
		int levelX=xStart;
		
		double minY=1.0;
		double maxY=0.0;
		
		for(int runIndex=0;runIndex<SplitSim.thresholds.size();runIndex++ ){
			if (SplitSim.thresholds.get(runIndex).getHitRatio()>maxY)
				maxY=SplitSim.thresholds.get(runIndex).getHitRatio();
			if (SplitSim.thresholds.get(runIndex).getHitRatio()<minY)
				minY=SplitSim.thresholds.get(runIndex).getHitRatio();
		}

		canvas.setColor(Color.black);
		double yValue = minY;
		double yValueRise = (maxY-minY)/10;
		int yPos = yStart;
		int yJump = (yStart-yEnd)/10;
		for(int i=1; i<=11;i++){
			canvas.drawString(Double.toString((double)Math.round(yValue* 10000) / 10000)+"-",xStart-50,yPos+3);
			yValue=yValue+yValueRise;
			yPos=yPos-yJump;
		}
		
		canvas.drawString("Hit Ratio",xStart-50,yEnd-20);
		canvas.drawString("Threshold Levels",xStart+400,yStart+40);
		canvas.drawString("Hit Ratio By Threshold Level",xStart+400,yEnd-20);
		
		int divs=400;
		double risePerDiv= (maxY-minY)/divs;
		double divPerRise = 1/risePerDiv;
		double currentRise = 0.0;
		double lastRise = 0.0;
		int currentY=0;
		int lastY=0;
		
		
		for(int runIndex=1;runIndex<SplitSim.thresholds.size();runIndex++ ){
			canvas.setColor(Color.GREEN);
			levelX=levelX+levelSpan;
			lastRise=SplitSim.thresholds.get(runIndex-1).getHitRatio()-minY;
			currentRise=SplitSim.thresholds.get(runIndex).getHitRatio()-minY;
			lastY=yStart-(int)(lastRise*divPerRise);
			currentY=yStart-(int)(currentRise*divPerRise);
			canvas.drawLine(levelX-levelSpan, lastY,levelX,currentY );
			if((runIndex%(SplitSim.splitCount/10))==0){
				canvas.setColor(Color.black);
				canvas.drawString("| L"+runIndex,levelX,yStart+10);
			}
		}

	}
	 
	
}
